
const util = require('util');
const fs = require('fs');

// 输出个检测日志方便查看
var logPath = './console.log';
var logFile = fs.createWriteStream(logPath, { flags: 'w' }); // flags: w 代表从头开始写即覆盖原有的， a 代表从末尾开始，接着原有的写

function formatDate() {
    // 单位数转化双位数
    var num = function (n) {
        return n < 10 ? '0' + n : n;
    }
    // 输出yyyy-MM-dd hh:mm:ss格式的时间
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return year + "-" +num(month) + "-" + num(day) + " " + num(hour) + ":" + num(minute);
}

module.exports = function () {
    logFile.write(formatDate() + '  ' + util.format.apply(null, arguments) + '\n');
    process.stdout.write(formatDate() + '  ' + util.format.apply(null, arguments) + '\n');
}
